package com.sugar.follow.up.module.business.service.patienthl;

import com.sugar.follow.up.module.business.controller.admin.patient.hl.vo.PatientHlPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.hl.vo.PatientHlSaveReqVO;
import com.sugar.follow.up.module.system.api.user.AdminUserApi;
import com.sugar.follow.up.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.sugar.follow.up.module.business.dal.dataobject.patienthl.PatientHlDO;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;

import com.sugar.follow.up.module.business.dal.mysql.patienthl.PatientHlMapper;

import static com.sugar.follow.up.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.follow.up.framework.common.util.collection.CollectionUtils.convertList;
import static com.sugar.follow.up.module.business.enums.ErrorCodeConstants.*;

/**
 * 化疗 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class PatientHlServiceImpl implements PatientHlService {

    @Resource
    private PatientHlMapper patientHlMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createPatientHl(PatientHlSaveReqVO createReqVO, Long adminUserId) {
        // 获取医生
        AdminUserRespDTO user = adminUserApi.getUser(adminUserId);

        // 插入
        PatientHlDO patientHl = BeanUtils.toBean(createReqVO, PatientHlDO.class);
        patientHl.setDoctorId(adminUserId).setDoctorName(user.getNickname());
        patientHlMapper.insert(patientHl);

        // 返回
        return patientHl.getId();
    }

    @Override
    public void updatePatientHl(PatientHlSaveReqVO updateReqVO) {
        // 校验存在
        validatePatientHlExists(updateReqVO.getId());
        // 更新
        PatientHlDO updateObj = BeanUtils.toBean(updateReqVO, PatientHlDO.class);
        patientHlMapper.updateById(updateObj);
    }

    @Override
    public void deletePatientHl(Long id) {
        // 校验存在
        validatePatientHlExists(id);
        // 删除
        patientHlMapper.deleteById(id);
    }

    @Override
        public void deletePatientHlListByIds(List<Long> ids) {
        // 删除
        patientHlMapper.deleteByIds(ids);
        }


    private void validatePatientHlExists(Long id) {
        if (patientHlMapper.selectById(id) == null) {
            throw exception(PATIENT_HL_NOT_EXISTS);
        }
    }

    @Override
    public PatientHlDO getPatientHl(Long id) {
        return patientHlMapper.selectById(id);
    }

    @Override
    public PageResult<PatientHlDO> getPatientHlPage(PatientHlPageReqVO pageReqVO) {
        return patientHlMapper.selectPage(pageReqVO);
    }

}